const axios = require('axios')
const qs = require('qs')
const getHeader = require('./getHeader')
const getDetails = require('./getDetails')

async function fetchData(page, maxPage) {
  const url =
    'https://my.cbg.163.com/cgi-bin/recommend.py?client_type=h5&act=recommd_by_role&search_type=role&count=15&view_loc=search_cond&equip_special_skill_logic=and&equip_special_effect_logic=and&own_fabaos_logic=or&shen_shou_logic=and&ling_shou_logic=and&zhen_shou_logic=and&mount_list_logic=and&fashion_list_logic=and&equip_level_min=90&equip_level_max=119&role_score=20000&order_by=&page=1&exter=www.baidu.com&page_session_id=01906396-6394-6666-4366-2135DB2E494F&_=1719657699104'
  const timeMillis = Date.now()

  const form = {
    client_type: 'h5',
    act: 'recommd_by_role',
    search_type: 'role',
    page: page.toString(), // 当前页码
    count: '15', // 每页条数
    view_loc: 'equip_list',
    order_by: '',
    exter: 'direct',
    page_session_id: '0190577E-61D1-6E71-D0E2-2232B45F4D11', // 未知参数
    _: timeMillis
  }

  const headers = getHeader(timeMillis)

  try {
    const response = await axios.get(url, {
      headers: headers,
      params: form,
      paramsSerializer: (params) => qs.stringify(params, { encode: false })
    })
    const result = response.data
    if (result.status != 1) {
      console.log(result.msg)
      return
    } else {
      // getDetails(result.result[0])
      result.result.forEach((item) => {
        getDetails(item)
      })
    }
  } catch (error) {
    console.error(`Error processing page ${page}:`, error)
  }
}

async function main() {
  const maxPage = 100 // 你想要累加到的最大页数
  for (let page = 1; page <= maxPage; page++) {
    await fetchData(page, maxPage)
  }
}

main()
